﻿<Page
    RequestedTheme="Light"
    x:Class="wToggl.ReportsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:wToggl"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:wToggle.Platform.Data"
    xmlns:windowsUniversal="using:OxyPlot.WindowsUniversal"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <ResourceDictionary>
            <local:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
            <DataTemplate x:Key="HubSectionHeaderTemplate">
                <TextBlock Margin="0,0,0,-9.5" Text="{Binding}"/>
            </DataTemplate>
        </ResourceDictionary>
    </Page.Resources>

    <Page.BottomAppBar>
        <CommandBar ClosedDisplayMode="Compact">
            <CommandBar.PrimaryCommands>
                <AppBarButton Icon="Find" Visibility="Visible" IsEnabled="True" Label="Filter" Name="SearchButton" Click="SearchButton_OnClick" />
                <AppBarButton Icon="Download" Visibility="Visible" IsEnabled="True" Label="PDF" Name="PdfButton" Click="PdfButton_OnClick" />
                <AppBarButton Icon="Mail" Visibility="Visible" IsEnabled="True" Label="Send" Name="SendPdfButton" Click="SendPdfButton_OnClick" />
            </CommandBar.PrimaryCommands>
        </CommandBar>
    </Page.BottomAppBar>
    
    <Grid x:Name="LayoutRoot">
        <local:BackgroundWorkOverlay Canvas.ZIndex="10" IsVisible="{Binding IsWorking}" OverlayMessage="{Binding OverlayMessage}"/>
        <Hub x:Name="Hub" Header="reports">
            <HubSection Header="SEARCH">
                <HubSection.ContentTemplate>
                    <DataTemplate>
                        <ScrollViewer>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid Grid.Row="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <DatePicker Header="From" Date="{Binding StartDate, Mode=TwoWay}"/>
                                    <DatePicker Grid.Column="1" Header="To" Date="{Binding EndDate, Mode=TwoWay}"/>
                                </Grid>
                                <local:TagSelector Header="Clients" PlaceholderText="Select clients" Grid.Row="2" Items="{Binding Workspace.Clients}" SelectedItems="{Binding SelectedClients, Mode=TwoWay}">
                                    <local:TagSelector.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}"/>
                                        </DataTemplate>
                                    </local:TagSelector.ItemTemplate>
                                </local:TagSelector>

                                <local:TagSelector Header="Projects" IsMultiSelect="True" Grid.Row="3" PlaceholderText="Select projects" Items="{Binding Workspace.Projects}" SelectedItems="{Binding SelectedProjects, Mode=TwoWay}">
                                    <local:TagSelector.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}"/>
                                        </DataTemplate>
                                    </local:TagSelector.ItemTemplate>
                                </local:TagSelector>

                                <local:TagSelector Header="Tags" PlaceholderText="Select tags" Grid.Row="4" Items="{Binding Workspace.Tags}" SelectedItems="{Binding SelectedTags, Mode=TwoWay}">
                                    <local:TagSelector.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}"/>
                                        </DataTemplate>
                                    </local:TagSelector.ItemTemplate>
                                </local:TagSelector>
                            </Grid>
                        </ScrollViewer>
                        
                    </DataTemplate>
                </HubSection.ContentTemplate>
            </HubSection>
            
            <HubSection Header="BREAKDOWNS">
                <HubSection.ContentTemplate>
                    <DataTemplate>
                        <Grid Visibility="{Binding Entries, Converter={StaticResource NullToVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid x:Name="ChartNavigationGrid">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock VerticalAlignment="Center" FontWeight="Bold" Name="ProjectChart" Tapped="ProjectChart_OnTapped" FontSize="25" Text="By project" HorizontalAlignment="Center"/>
                                <TextBlock VerticalAlignment="Center" Name="ClientChart" Tapped="ClientChart_OnTapped" Grid.Column="2" FontSize="18" Text="By client" HorizontalAlignment="Center"/>
                                <TextBlock VerticalAlignment="Center" Name="DateChart" Tapped="DateChart_OnTapped" Grid.Column="4" FontSize="18" Text="By date" HorizontalAlignment="Center"/>
                            </Grid>
                            <FlipView Grid.Row="1" SelectedIndex="{Binding ChartIndex, Mode=TwoWay}" SelectionChanged="Selector_OnSelectionChanged">
                                <FlyoutBase.AttachedFlyout>
                                    <Flyout Placement="Top">
                                        <Grid Margin="10,5">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.ColumnSpan="2" FontSize="22" Style="{StaticResource BodyTextBlockStyle}" FontWeight="Bold" Text="{Binding SelectedWorkTitle}"/>
                                            <TextBlock Grid.Column="0" Grid.Row="1" Style="{StaticResource BodyTextBlockStyle}" Text="Work time:"/>
                                            <TextBlock Grid.Column="0" Grid.Row="2" Style="{StaticResource BodyTextBlockStyle}" Text="Portion of total:"/>
                                            <TextBlock Grid.Column="1" HorizontalAlignment="Right" Grid.Row="1" Style="{StaticResource BodyTextBlockStyle}" Text="{Binding SelectedWorkTime}"/>
                                            <TextBlock Grid.Column="1" HorizontalAlignment="Right" Grid.Row="2" Style="{StaticResource BodyTextBlockStyle}" Text="{Binding SelectedWorkPortion}"/>
                                        </Grid>
                                    </Flyout>
                                </FlyoutBase.AttachedFlyout>
                                <windowsUniversal:PlotView Height="260" IsHitTestVisible="False" ManipulationMode="None" IsTabStop="False" Model="{Binding ProjectData}"/>
                                <windowsUniversal:PlotView Height="260" IsHitTestVisible="False" ManipulationMode="None" IsTabStop="False" Model="{Binding ClientData}"/>
                                <windowsUniversal:PlotView IsHitTestVisible="False" ManipulationMode="None" IsTabStop="False" Model="{Binding DateData}"/>
                            </FlipView>
                        </Grid>
                    </DataTemplate>
                </HubSection.ContentTemplate>
            </HubSection>
            
            <HubSection Header="ENTRIES">
                <HubSection.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" Margin="0,0,0,20">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock FontSize="22" FontWeight="Bold" HorizontalAlignment="Stretch" Style="{StaticResource BodyTextBlockStyle}" Text="Total:"/>
                                <TextBlock FontSize="22" HorizontalAlignment="Right" Grid.Column="1" Style="{StaticResource BodyTextBlockStyle}" Foreground="{StaticResource PhoneAccentBrush}" FontWeight="Bold" Text="{Binding WorkTotal}"/>
                            </Grid>
                            <ListView SelectionMode="Single" Grid.Row="1" ItemsSource="{Binding Entries}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
                                <ListView.ItemContainerStyle>
                                    <Style TargetType="ListViewItem">
                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                    </Style>
                                </ListView.ItemContainerStyle>
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid IsTapEnabled="True" Name="TimeEntryGrid"  Margin="0,5" d:DataContext="{d:DesignInstance data:TimeEntry}" HorizontalAlignment="Stretch">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition Width="Auto"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <TextBlock FontWeight="Bold" Grid.Row="0" HorizontalAlignment="Stretch" Style="{StaticResource BodyTextBlockStyle}" Text="{Binding ReadableDescription}"/>
                                            <TextBlock Grid.Row="0" FontWeight="Bold" HorizontalAlignment="Right" Style="{StaticResource BodyTextBlockStyle}" Grid.Column="1" Text="{Binding DurationString}"/>
                                            <TextBlock Grid.Row="1" Style="{StaticResource TimeEntrySubitem}" Grid.Column="0" Text="{Binding Project.Name}" HorizontalAlignment="Stretch" />
                                            <TextBlock MaxWidth="200" TextTrimming="CharacterEllipsis" Grid.Row="1" Style="{StaticResource TimeEntrySubitem}" Grid.Column="1" Text="{Binding TagString}" HorizontalAlignment="Right"/>
                                        </Grid>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </Grid>
                    </DataTemplate>
                </HubSection.ContentTemplate>
            </HubSection>
        </Hub>
    </Grid>
</Page>
